package forum.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import forum.logs.LogForum;

import forum.dao.LoginDAO;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(name = "login", urlPatterns = { "/forum/login" })
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int acao = -1;
		
		try {
			acao = Integer.valueOf(request.getParameter("acao"));
		} catch (NumberFormatException numberFormatException) {
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
		
		switch (acao) {
			case 1:
				this.validaLogin(request, response);
				break;
			case 2:
				LoginServlet.logado(request, response, "admin/administrador.jsp");
				break;
			case 3:
				this.logoff(request, response);
		}
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);
	}
	
	private void validaLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String usuario = request.getParameter("usuario");
		String senha = request.getParameter("senha");
		
		if (new LoginDAO().valido(usuario, senha)) {
			request.setAttribute("usuario", usuario);
			request.getSession().setAttribute("usuario", usuario);
			
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("admin/administrador.jsp");
			requestDispatcher.forward(request, response);
		} else {
			response.sendRedirect("login.jsp");
		}
	}
	
	private void log(HttpServletRequest request, String usuario) {
		request.getSession().setAttribute("usuario", usuario);
	}
	
	public static void logado(HttpServletRequest request, HttpServletResponse response, String endereco) throws ServletException, IOException {
		if (request.getSession().getAttribute("usuario") != null) {
			request.setAttribute("usuario", request.getSession().getAttribute("usuario"));
			request.getRequestDispatcher(endereco).forward(request, response);
		} else {
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}

	private void logoff(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession().invalidate();
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}
	
	public static void enviarErro(HttpServletRequest request, HttpServletResponse response, java.lang.Exception exception) throws ServletException, IOException {
		String erro = LogForum.getMenssagemErro(exception);
		erro = erro.replaceAll("\n", "<br />");
		
		request.setAttribute("codigo", 1);
		request.setAttribute("erro", erro);
		request.getRequestDispatcher("/erro.jsp").forward(request, response);
	}
}
